package com.java.base.exercise.current;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author RenPu
 * @Date 2021/9/22 10:52
 * @Version 1.0
 * @Description: 模拟一个场景，三个程序员都干完活了，才算完成项目
 **/
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        Worker worker = new Worker("程序员No.1", countDownLatch);
        Worker worker2 = new Worker("程序员No.2", countDownLatch);
        Worker worker3 = new Worker("程序员No.3", countDownLatch);
        worker.start();
        worker2.start();
        worker3.start();
        countDownLatch.await();


    }


}

class Worker extends Thread {

    private String workerName;

    private CountDownLatch countDownLatch;

    public Worker(String workerName, CountDownLatch countDownLatch) {
        this.workerName = workerName;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {

        try {
            System.out.println("Worker:" + workerName + "is begining.");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("Worker:" + workerName + "is end!!!!.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //模仿干活
        countDownLatch.countDown();

    }

}
